home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / binutils.252 / gas / config / tc-h8300.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-08  |  27.2 KB  |  1,489 lines

  1. /* tc-h8300.c -- Assemble code for the Hitachi H8/300
  2.    Copyright (C) 1991, 1992 Free Software Foundation.
  3.  
  4.    This file is part of GAS, the GNU Assembler.
  5.  
  6.    GAS is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    GAS is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with GAS; see the file COPYING.  If not, write to
  18.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. /*
  22.   Written By Steve Chamberlain
  23.   sac@cygnus.com
  24.   */
  25.  
  26. #include <stdio.h>
  27. #include "as.h"
  28. #include "bfd.h"
  29. #define DEFINE_TABLE
  30. #define h8_opcodes ops
  31. #include "opcode/h8300.h"
  32. #include <ctype.h>
  33.  
  34. const char comment_chars[] =
  35. {';', 0};
  36. const char line_separator_chars[] =
  37. {0};
  38. const char line_comment_chars[] = "#";
  39.  
  40. /* This table describes all the machine specific pseudo-ops the assembler
  41.    has to support.  The fields are:
  42.    pseudo-op name without dot
  43.    function to call to execute this pseudo-op
  44.    Integer arg to pass to the function
  45.    */
  46.  
  47. void cons ();
  48.  
  49. int Hmode;
  50. #define PSIZE (Hmode ? L_32 : L_16)
  51. #define DMODE (L_16)
  52. #define DSYMMODE (Hmode ? L_24 : L_16)
  53. int bsize = L_8;        /* default branch displacement */
  54.  
  55.  
  56. void
  57. h8300hmode ()
  58. {
  59.   Hmode = 1;
  60. }
  61.  
  62.  
  63. void
  64. sbranch (size)
  65.      int size;
  66. {
  67.   bsize = size;
  68. }
  69.  
  70. static void pint ()
  71. {
  72.   cons (Hmode ? 4 : 2);
  73. }
  74.  
  75. const pseudo_typeS md_pseudo_table[] =
  76. {
  77.  
  78.   {"h8300h", h8300hmode, 0},
  79.   {"sbranch", sbranch, L_8},
  80.   {"lbranch", sbranch, L_16},
  81.  
  82.   {"int", pint, 0},
  83.   {"data.b", cons, 1},
  84.   {"data.w", cons, 2},
  85.   {"data.l", cons, 4},
  86.   {"form", listing_psize, 0},
  87.   {"heading", listing_title, 0},
  88.   {"import", s_ignore, 0},
  89.   {"page", listing_eject, 0},
  90.   {"program", s_ignore, 0},
  91.   {0, 0, 0}
  92. };
  93.  
  94. const int md_reloc_size;
  95.  
  96. const char EXP_CHARS[] = "eE";
  97.  
  98. /* Chars that mean this number is a floating point constant */
  99. /* As in 0f12.456 */
  100. /* or    0d1.2345e12 */
  101. const char FLT_CHARS[] = "rRsSfFdDxXpP";
  102.  
  103.  
  104. const relax_typeS md_relax_table[1];
  105.  
  106.  
  107. static struct hash_control *opcode_hash_control;    /* Opcode mnemonics */
  108.  
  109. /*
  110.   This function is called once, at assembler startup time.  This should
  111.   set up all the tables, etc that the MD part of the assembler needs
  112.   */
  113.  
  114.  
  115. void
  116. md_begin ()
  117. {
  118.   struct h8_opcode *opcode;
  119.   const struct reg_entry *reg;
  120.   char prev_buffer[100];
  121.   int idx = 0;
  122.  
  123.   opcode_hash_control = hash_new ();
  124.   prev_buffer[0] = 0;
  125.  
  126.   for (opcode = h8_opcodes; opcode->name; opcode++)
  127.     {
  128.       /* Strip off any . part when inserting the opcode and only enter
  129.          unique codes into the hash table
  130.          */
  131.       char *src = opcode->name;
  132.       unsigned int len = strlen (src);
  133.       char *dst = malloc (len + 1);
  134.       char *buffer = dst;
  135.  
  136.       opcode->size = 0;
  137.       while (*src)
  138.     {
  139.       if (*src == '.')
  140.         {
  141.           src++;
  142.           opcode->size = *src;
  143.           break;
  144.         }
  145.       *dst++ = *src++;
  146.     }
  147.       *dst++ = 0;
  148.       if (strcmp (buffer, prev_buffer))
  149.     {
  150.       hash_insert (opcode_hash_control, buffer, (char *) opcode);
  151.       strcpy (prev_buffer, buffer);
  152.       idx++;
  153.     }
  154.       opcode->idx = idx;
  155.  
  156.  
  157.       /* Find the number of operands */
  158.       opcode->noperands = 0;
  159.       while (opcode->args.nib[opcode->noperands] != E)
  160.     opcode->noperands++;
  161.       /* Find the length of the opcode in bytes */
  162.       opcode->length = 0;
  163.       while (opcode->data.nib[opcode->length * 2] != E)
  164.     opcode->length++;
  165.     }
  166.  
  167. }
  168.  
  169.  
  170. struct h8_exp
  171. {
  172.   char *e_beg;
  173.   char *e_end;
  174.   expressionS e_exp;
  175. };
  176. int dispreg;
  177. int opsize;            /* Set when a register size is seen */
  178.  
  179.  
  180. struct h8_op
  181. {
  182.   op_type mode;
  183.   unsigned reg;
  184.   expressionS exp;
  185. };
  186.  
  187. /*
  188.   parse operands
  189.   WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
  190.   r0l,r0h,..r7l,r7h
  191.   @WREG
  192.   @WREG+
  193.   @-WREG
  194.   #const
  195.  
  196.   */
  197.  
  198. /* try and parse a reg name, returns number of chars consumed */
  199. int
  200. parse_reg (src, mode, reg, direction)
  201.      char *src;
  202.      op_type *mode;
  203.      unsigned int *reg;
  204.      int direction;
  205.  
  206. {
  207.   if (src[0] == 's' && src[1] == 'p')
  208.     {
  209.       *mode = PSIZE | REG | direction;
  210.       *reg = 7;
  211.       return 2;
  212.     }
  213.   if (src[0] == 'c' && src[1] == 'c' && src[2] == 'r')
  214.     {
  215.       *mode = CCR;
  216.       *reg = 0;
  217.       return 3;
  218.     }
  219.   if (src[0] == 'f' && src[1] == 'p')
  220.     {
  221.       *mode = PSIZE | REG | direction;
  222.       *reg = 6;
  223.       return 2;
  224.     }
  225.   if (src[0] == 'e'
  226.       && src[1] == 'r'
  227.       && src[2] >= '0' && src[2] <= '7')
  228.     {
  229.       *mode = L_32 | REG | direction;
  230.       *reg = src[2] - '0';
  231.       if (!Hmode)
  232.     as_warn ("Reg only legal for H8/300-H");
  233.  
  234.       return 3;
  235.     }
  236.   if (src[0] == 'e'
  237.       && src[1] >= '0' && src[1] <= '7')
  238.     {
  239.       *mode = L_16 | REG | direction;
  240.       *reg = src[1] - '0' + 8;
  241.       if (!Hmode)
  242.     as_warn ("Reg only legal for H8/300-H");
  243.       return 2;
  244.     }
  245.  
  246.   if (src[0] == 'r')
  247.     {
  248.       if (src[1] >= '0' && src[1] <= '7')
  249.     {
  250.       if (src[2] == 'l')
  251.         {
  252.           *mode = L_8 | REG | direction;
  253.           *reg = (src[1] - '0') + 8;
  254.           return 3;
  255.         }
  256.       if (src[2] == 'h')
  257.         {
  258.           *mode = L_8 | REG | direction;
  259.           *reg = (src[1] - '0');
  260.           return 3;
  261.         }
  262.       *mode = L_16 | REG | direction;
  263.       *reg = (src[1] - '0');
  264.       return 2;
  265.     }
  266.     }
  267.   return 0;
  268. }
  269.  
  270. char *
  271. DEFUN (parse_exp, (s, op),
  272.        char *s AND
  273.        expressionS * op)
  274. {
  275.   char *save = input_line_pointer;
  276.   char *new;
  277.  
  278.   input_line_pointer = s;
  279.   expression (op);
  280.   if (op->X_op == O_absent)
  281.     as_bad ("missing operand");
  282.   new = input_line_pointer;
  283.   input_line_pointer = save;
  284.   return new;
  285. }
  286.  
  287. static char *
  288. skip_colonthing (ptr, exp, mode)
  289.      char *ptr;
  290.      expressionS *exp;
  291.      int *mode;
  292. {
  293.   if (*ptr == ':')
  294.     {
  295.       ptr++;
  296.       if (*ptr == '8')
  297.     {
  298.       ptr++;
  299.       /* ff fill any 8 bit quantity */
  300.      /* exp->X_add_number -= 0x100;*/
  301.     }
  302.       else
  303.     {
  304.       *mode &= ~SIZE;
  305.       if (*ptr == '2')
  306.         {
  307.           *mode |= L_24;
  308.         }
  309.       else if (*ptr == '1')
  310.         {
  311.           *mode |= L_16;
  312.         }
  313.       while (isdigit (*ptr))
  314.         ptr++;
  315.     }
  316.     }
  317.   return ptr;
  318. }
  319.  
  320. /* The many forms of operand:
  321.  
  322.    Rn            Register direct
  323.    @Rn            Register indirect
  324.    @(exp[:16], Rn)    Register indirect with displacement
  325.    @Rn+
  326.    @-Rn
  327.    @aa:8            absolute 8 bit
  328.    @aa:16            absolute 16 bit
  329.    @aa            absolute 16 bit
  330.  
  331.    #xx[:size]        immediate data
  332.    @(exp:[8], pc)        pc rel
  333.    @@aa[:8]        memory indirect
  334.  
  335.    */
  336.  
  337. char *
  338. colonmod24 (op, src)
  339.      struct h8_op *op;
  340.      char *src;
  341.  
  342. {
  343.   int mode = 0;
  344.   src = skip_colonthing (src, &op->exp, &mode);
  345.  
  346.   if (!mode)
  347.     {
  348.       /* Choose a default mode */
  349.       if (op->exp.X_add_number < -32768
  350.       || op->exp.X_add_number > 32767)
  351.     {
  352.       if (Hmode)
  353.         mode = L_24;
  354.       else
  355.         mode = L_16;
  356.     }
  357.       else if (op->exp.X_add_symbol
  358.            || op->exp.X_op_symbol)
  359.     mode = DSYMMODE;
  360.       else
  361.     mode = DMODE;
  362.     }
  363.   op->mode |= mode;
  364.   return src;
  365.  
  366. }
  367.  
  368.  
  369. static void
  370. get_operand (ptr, op, dst, direction)
  371.      char **ptr;
  372.      struct h8_op *op;
  373.      unsigned int dst;
  374.  
  375. {
  376.   char *src = *ptr;
  377.   op_type mode;
  378.   unsigned int num;
  379.   unsigned int len;
  380.   unsigned int size;
  381.  
  382.   op->mode = E;
  383.  
  384.   len = parse_reg (src, &op->mode, &op->reg, direction);
  385.   if (len)
  386.     {
  387.       *ptr = src + len;
  388.       return;
  389.     }
  390.  
  391.   if (*src == '@')
  392.     {
  393.       src++;
  394.       if (*src == '@')
  395.     {
  396.       src++;
  397.       src = parse_exp (src, &op->exp);
  398.  
  399.       src = skip_colonthing (src, &op->exp, &op->mode);
  400.  
  401.       *ptr = src;
  402.  
  403.       op->mode = MEMIND;
  404.       return;
  405.  
  406.     }
  407.  
  408.  
  409.       if (*src == '-')
  410.     {
  411.       src++;
  412.       len = parse_reg (src, &mode, &num, direction);
  413.       if (len == 0)
  414.         {
  415.           /* Oops, not a reg after all, must be ordinary exp */
  416.           src--;
  417.           /* must be a symbol */
  418.           op->mode = ABS | PSIZE | direction;
  419.           *ptr = skip_colonthing (parse_exp (src, &op->exp),
  420.                       &op->exp, &op->mode);
  421.  
  422.           return;
  423.  
  424.  
  425.         }
  426.  
  427.  
  428.       if ((mode & SIZE) != PSIZE)
  429.         as_bad ("Wrong size pointer register for architecture.");
  430.       op->mode = RDDEC;
  431.       op->reg = num;
  432.       *ptr = src + len;
  433.       return;
  434.     }
  435.       if (*src == '(')
  436.     {
  437.       /* Disp */
  438.       src++;
  439.  
  440.       /* Start off assuming a 16 bit offset */
  441.  
  442.  
  443.       src = parse_exp (src, &op->exp);
  444.  
  445.       src = colonmod24 (op, src);
  446.  
  447.       if (*src == ')')
  448.         {
  449.           src++;
  450.           op->mode |= ABS | direction;
  451.           *ptr = src;
  452.           return;
  453.         }
  454.  
  455.       if (*src != ',')
  456.         {
  457.           as_bad ("expected @(exp, reg16)");
  458.           return;
  459.  
  460.         }
  461.       src++;
  462.  
  463.       len = parse_reg (src, &mode, &op->reg, direction);
  464.       if (len == 0 || !(mode & REG))
  465.         {
  466.           as_bad ("expected @(exp, reg16)");
  467.           return;
  468.         }
  469.       op->mode |= DISP | direction;
  470.       dispreg = op->reg;
  471.       src += len;
  472.       src = skip_colonthing (src, &op->exp, &op->mode);
  473.  
  474.       if (*src != ')' && '(')
  475.         {
  476.           as_bad ("expected @(exp, reg16)");
  477.           return;
  478.         }
  479.       *ptr = src + 1;
  480.  
  481.       return;
  482.     }
  483.       len = parse_reg (src, &mode, &num, direction);
  484.  
  485.       if (len)
  486.     {
  487.       src += len;
  488.       if (*src == '+')
  489.         {
  490.           src++;
  491.           if ((mode & SIZE) != PSIZE)
  492.         as_bad ("Wrong size pointer register for architecture.");
  493.           op->mode = RSINC;
  494.           op->reg = num;
  495.           *ptr = src;
  496.           return;
  497.         }
  498.       if ((mode & SIZE) != PSIZE)
  499.         as_bad ("Wrong size pointer register for architecture.");
  500.  
  501.       op->mode = direction | IND | PSIZE;
  502.       op->reg = num;
  503.       *ptr = src;
  504.  
  505.       return;
  506.     }
  507.       else
  508.     {
  509.       /* must be a symbol */
  510.  
  511.       op->mode = ABS | direction;
  512.       src = parse_exp (src, &op->exp);
  513.  
  514.       *ptr = colonmod24 (op, src);
  515.  
  516.       return;
  517.     }
  518.     }
  519.  
  520.  
  521.   if (*src == '#')
  522.     {
  523.       src++;
  524.       op->mode = IMM;
  525.       src = parse_exp (src, &op->exp);
  526.       *ptr = skip_colonthing (src, &op->exp, &op->mode);
  527.  
  528.       return;
  529.     }
  530.   else
  531.     {
  532.       src = parse_exp (src, &op->exp);
  533.       /* Trailing ':' size ? */
  534.       if (*src == ':')
  535.     {
  536.       if (src[1] == '1' && src[2] == '6')
  537.         {
  538.           op->mode = PCREL | L_16;
  539.           src += 3;
  540.         }
  541.       else if (src[1] == '8')
  542.         {
  543.           op->mode = PCREL | L_8;
  544.           src += 2;
  545.         }
  546.       else
  547.         {
  548.           as_bad ("expect :8 or :16 here");
  549.         }
  550.     }
  551.       else
  552.     {
  553.       op->mode = PCREL | bsize;
  554.     }
  555.       *ptr = src;
  556.     }
  557. }
  558.  
  559.  
  560. static
  561. char *
  562. DEFUN (get_operands, (noperands, op_end, operand),
  563.        unsigned int noperands AND
  564.        char *op_end AND
  565.        struct h8_op *operand)
  566. {
  567.   char *ptr = op_end;
  568.  
  569.   switch (noperands)
  570.     {
  571.     case 0:
  572.       operand[0].mode = 0;
  573.       operand[1].mode = 0;
  574.       break;
  575.  
  576.     case 1:
  577.       ptr++;
  578.       get_operand (&ptr, operand + 0, 0, SRC);
  579.       if (*ptr == ',')
  580.     {
  581.       ptr++;
  582.       get_operand (&ptr, operand + 1, 1, DST);
  583.     }
  584.       else
  585.     {
  586.       operand[1].mode = 0;
  587.     }
  588.  
  589.       break;
  590.     case 2:
  591.       ptr++;
  592.       get_operand (&ptr, operand + 0, 0, SRC);
  593.       if (*ptr == ',')
  594.     ptr++;
  595.       get_operand (&ptr, operand + 1, 1, DST);
  596.       break;
  597.  
  598.     default:
  599.       abort ();
  600.     }
  601.  
  602.  
  603.   return ptr;
  604. }
  605.  
  606. /* Passed a pointer to a list of opcodes which use different
  607.    addressing modes, return the opcode which matches the opcodes
  608.    provided
  609.    */
  610. static
  611. struct h8_opcode *
  612. get_specific (opcode, operands)
  613.      struct h8_opcode *opcode;
  614.      struct h8_op *operands;
  615. {
  616.   struct h8_opcode *this_try = opcode;
  617.   int found = 0;
  618.  
  619.   unsigned int this_index = opcode->idx;
  620.  
  621.   while (this_index == opcode->idx && !found)
  622.     {
  623.       unsigned int i;
  624.       found = 1;
  625.  
  626.       this_try = opcode++;
  627.       for (i = 0; i < this_try->noperands && found; i++)
  628.     {
  629.       op_type op = this_try->args.nib[i];
  630.       int x = operands[i].mode;
  631.  
  632.       if ((op & (DISP | REG)) == (DISP | REG)
  633.           && ((x & DISP | REG) == (DISP | REG)))
  634.         {
  635.           dispreg = operands[i].reg;
  636.         }
  637.       else if (op & REG)
  638.         {
  639.           if (!(x & REG))
  640.         found = 0;
  641.  
  642.           if (x & L_P)
  643.         {
  644.           x = (x & ~L_P) | (Hmode ? L_32 : L_16);
  645.         }
  646.           if (op & L_P)
  647.         {
  648.           op = (op & ~L_P) | (Hmode ? L_32 : L_16);
  649.         }
  650.  
  651.           opsize = op & SIZE;
  652.  
  653.           /* The size of the reg is v important */
  654.           if ((op & SIZE) != (x & SIZE))
  655.         found = 0;
  656.         }
  657.       else if ((op & ABSJMP) && (x & ABS))
  658.         {
  659.           operands[i].mode &= ~ABS;
  660.           operands[i].mode |= ABSJMP;
  661.           /* But it may not be 24 bits long */
  662.           if (!Hmode)
  663.         {
  664.           operands[i].mode &= ~SIZE;
  665.           operands[i].mode |= L_16;
  666.         }
  667.  
  668.  
  669.         }
  670.       else if ((op & (KBIT | DBIT)) && (x & IMM))
  671.         {
  672.           /* This is ok if the immediate value is sensible */
  673.  
  674.         }
  675.       else if (op & PCREL)
  676.         {
  677.  
  678.           /* The size of the displacement is important */
  679.           if ((op & SIZE) != (x & SIZE))
  680.         found = 0;
  681.  
  682.         }
  683.       else if ((op & (DISP | IMM | ABS))
  684.            && (op & (DISP | IMM | ABS)) == (x & (DISP | IMM | ABS)))
  685.         {
  686.           /* Got a diplacement,will fit if no size or same size as try */
  687.           if (op & ABS && op & L_8) 
  688.         {
  689.           /* We want an 8 bit abs here, but one which looks like 16 bits will do fine */
  690.           if (x & L_16)
  691.             found= 1;
  692.         }
  693.           else
  694.           if ((x & SIZE) != 0
  695.           && ((op & SIZE) != (x & SIZE)))
  696.         found = 0;
  697.         }
  698. #if 0
  699.       else if ((op & ABSMOV) && (x & ABS))
  700.         {
  701.           /* An absmov is only
  702.           /* Ok */
  703.         }
  704. #endif
  705.       else if ((op & MODE) != (x & MODE))
  706.         {
  707.           found = 0;
  708.         }
  709.  
  710.     }
  711.     }
  712.   if (found)
  713.     return this_try;
  714.   else
  715.     return 0;
  716. }
  717.  
  718. static void
  719. check_operand (operand, width, string)
  720.      struct h8_op *operand;
  721.      unsigned int width;
  722.      char *string;
  723. {
  724.   if (operand->exp.X_add_symbol == 0
  725.       && operand->exp.X_op_symbol == 0)
  726.     {
  727.  
  728.       /* No symbol involved, let's look at offset, it's dangerous if any of
  729.      the high bits are not 0 or ff's, find out by oring or anding with
  730.      the width and seeing if the answer is 0 or all fs*/
  731.       
  732.       if ((operand->exp.X_add_number & ~width) != 0 &&
  733.       (operand->exp.X_add_number | width) != (~0))
  734.     {
  735.       if (width == 255 
  736.           && (operand->exp.X_add_number & 0xff00) == 0xff00)
  737.         {
  738.           /* Just ignore this one - which happens when trying to 
  739.           fit a 16 bit address truncated into an 8 bit address of something like bset  */
  740.         }
  741.       else 
  742.         {
  743.       as_warn ("operand %s0x%x out of range.", string, operand->exp.X_add_number);
  744.     }
  745.     }
  746.     }
  747.  
  748. }
  749.  
  750. static void
  751. do_a_fix_imm (offset, operand, relaxing)
  752.      int offset;
  753.      struct h8_op *operand;
  754.      int relaxing;
  755. {
  756.   int idx;
  757.   int size;
  758.   int where;
  759.  
  760.  
  761.   char *t = operand->mode & IMM ? "#" : "@";
  762.  
  763.   if (operand->exp.X_add_symbol == 0)
  764.     {
  765.       char *bytes = frag_now->fr_literal + offset;
  766.       switch (operand->mode & SIZE)
  767.     {
  768.     case L_2:
  769.       check_operand (operand, 0x3, t);
  770.       bytes[0] |= (operand->exp.X_add_number) << 4;
  771.       break;
  772.     case L_3:
  773.       check_operand (operand, 0x7, t);
  774.       bytes[0] |= (operand->exp.X_add_number) << 4;
  775.       break;
  776.     case L_8:
  777.       check_operand (operand, 0xff, t);
  778.       bytes[0] = operand->exp.X_add_number;
  779.       break;
  780.     case L_16:
  781.       check_operand (operand, 0xffff, t);
  782.       bytes[0] = operand->exp.X_add_number >> 8;
  783.       bytes[1] = operand->exp.X_add_number >> 0;
  784.       break;
  785.     case L_24:
  786.       check_operand (operand, 0xffffff, t);
  787.       bytes[0] = operand->exp.X_add_number >> 16;
  788.       bytes[1] = operand->exp.X_add_number >> 8;
  789.       bytes[2] = operand->exp.X_add_number >> 0;
  790.       break;
  791.  
  792.     case L_32:
  793.       /* This should be done with bfd */
  794.       bytes[0] = operand->exp.X_add_number >> 24;
  795.       bytes[1] = operand->exp.X_add_number >> 16;
  796.       bytes[2] = operand->exp.X_add_number >> 8;
  797.       bytes[3] = operand->exp.X_add_number >> 0;
  798.       break;
  799.     }
  800.  
  801.     }
  802.   else
  803.     {
  804.       switch (operand->mode & SIZE)
  805.     {
  806.  
  807.     case L_24:
  808.       size = 4;
  809.       where = -1;
  810.       idx = relaxing ? R_MOVLB1 : R_RELLONG;
  811.       break;
  812.     default:
  813.       as_bad("Can't work out size of operand.\n");
  814.     case L_32:
  815.       size = 4;
  816.       where = 0;
  817.       idx = R_RELLONG;
  818.       break;
  819.     case L_16:
  820.       size = 2;
  821.       where = 0;
  822.       idx = relaxing ? R_MOVB1 : R_RELWORD;
  823.       break;
  824.     case L_8:
  825.       size = 1;
  826.       where = 0;
  827.       idx = R_RELBYTE;
  828.     }
  829.  
  830.       /* Sign extend any expression */
  831.       operand->exp.X_add_number = (short)operand->exp.X_add_number;
  832.       fix_new_exp (frag_now,
  833.            offset + where,
  834.            size,
  835.            &operand->exp,
  836.            0,
  837.            idx);
  838.     }
  839.  
  840. }
  841.  
  842. /* Now we know what sort of opcodes it is, lets build the bytes -
  843.  */
  844. static void
  845. build_bytes (this_try, operand)
  846.      struct h8_opcode *this_try;
  847.      struct h8_op *operand;
  848. {
  849.   unsigned int i;
  850.  
  851.   char *output = frag_more (this_try->length);
  852.   char *output_ptr = output;
  853.   op_type *nibble_ptr = this_try->data.nib;
  854.   char part;
  855.   op_type c;
  856.   char high;
  857.   unsigned int nibble_count = 0;
  858.   int absat;
  859.   int immat;
  860.   int nib;
  861.   char asnibbles[30];
  862.   char *p = asnibbles;
  863.  
  864.   if (!(this_try->inbase || Hmode))
  865.     {
  866.       as_warn ("Opcode `%s' only available on H8/300-H", this_try->name);
  867.     }
  868.  
  869.   while (*nibble_ptr != E)
  870.     {
  871.       int d;
  872.       c = *nibble_ptr++;
  873.  
  874.       d = (c & DST) != 0;
  875.  
  876.       if (c < 16)
  877.     {
  878.       nib = c;
  879.     }
  880.       else
  881.     {
  882.  
  883.       if (c & (REG | IND | INC | DEC))
  884.         {
  885.           nib = operand[d].reg;
  886.         }
  887.       else if ((c & DISPREG) == (DISPREG))
  888.         {
  889.           nib = dispreg;
  890.         }
  891.  
  892.       else if (c & ABSMOV)
  893.         {
  894.           operand[d].mode &= ~ABS;
  895.           operand[d].mode |= ABSMOV;
  896.           immat = nibble_count / 2;
  897.           nib = 0;
  898.         }
  899.       else if (c &  ABS )
  900.         {
  901.           operand[d].mode = c;
  902.           absat = nibble_count / 2;
  903.           nib = 0;
  904.         }
  905.       else if (c & (IMM | PCREL | ABS | ABSJMP | DISP))
  906.         {
  907.           operand[d].mode = c;
  908.           immat = nibble_count / 2;
  909.           nib = 0;
  910.         }
  911.       else if (c & IGNORE)
  912.         {
  913.           nib = 0;
  914.         }
  915.       else if (c & DBIT)
  916.         {
  917.           switch (operand[0].exp.X_add_number)
  918.         {
  919.         case 1:
  920.           nib = c;
  921.           break;
  922.         case 2:
  923.           nib = 0x8 | c;
  924.           break;
  925.         default:
  926.           as_bad ("Need #1 or #2 here");
  927.         }
  928.         }
  929.       else if (c & KBIT)
  930.         {
  931.           switch (operand[0].exp.X_add_number)
  932.         {
  933.         case 1:
  934.           nib = 0;
  935.           break;
  936.         case 2:
  937.           nib = 8;
  938.           break;
  939.         case 4:
  940.           if (!Hmode)
  941.             as_warn ("#4 only valid in h8/300 mode.");
  942.           nib = 9;
  943.           break;
  944.  
  945.         default:
  946.           as_bad ("Need #1 or #2 here");
  947.           break;
  948.         }
  949.           /* stop it making a fix */
  950.           operand[0].mode = 0;
  951.         }
  952.  
  953.       if (c & B31)
  954.         {
  955.           nib |= 0x8;
  956.         }
  957.     }
  958.       nibble_count++;
  959.  
  960.       *p++ = nib;
  961.     }
  962.  
  963.   for (i = 0; i < this_try->length; i++)
  964.     {
  965.       output[i] = (asnibbles[i * 2] << 4) | asnibbles[i * 2 + 1];
  966.     }
  967.  
  968.   /* output any fixes */
  969.   for (i = 0; i < 2; i++)
  970.     {
  971.       int x = operand[i].mode;
  972.  
  973.       if (x & (IMM | DISP))
  974.     {
  975.       do_a_fix_imm (output - frag_now->fr_literal + immat, operand + i, 0);
  976.     }
  977.       else if (x & ABS)
  978.     {
  979.       do_a_fix_imm (output - frag_now->fr_literal + absat, operand + i, 0);
  980.     }
  981.       else if (x & PCREL)
  982.     {
  983.       int size16 = x & L_16;
  984.       int where = size16 ? 2 : 1;
  985.       int size = size16 ? 2 : 1;
  986.       int type = size16 ? R_PCRWORD : R_PCRBYTE;
  987.  
  988.       check_operand (operand + i, size16 ? 0x7fff : 0x7f, "@");
  989.  
  990.       if (operand[i].exp.X_add_number & 1)
  991.         {
  992.           as_warn ("branch operand has odd offset (%x)\n",
  993.                operand->exp.X_add_number);
  994.         }
  995.  
  996.       operand[i].exp.X_add_number =
  997.         (char) (operand[i].exp.X_add_number - 1);
  998.       fix_new_exp (frag_now,
  999.                output - frag_now->fr_literal + where,
  1000.                size,
  1001.                &operand[i].exp,
  1002.                1,
  1003.                type);
  1004.     }
  1005.       else if (x & MEMIND)
  1006.     {
  1007.  
  1008.       check_operand (operand + i, 0xff, "@@");
  1009.       fix_new_exp (frag_now,
  1010.                output - frag_now->fr_literal + 1,
  1011.                1,
  1012.                &operand[i].exp,
  1013.                0,
  1014.                R_RELBYTE);
  1015.     }
  1016.  
  1017.       else if (x & ABSMOV)
  1018.     {
  1019.       /* This mov is either absolute long or thru a memory loc */
  1020.       do_a_fix_imm (output - frag_now->fr_literal + immat, operand + i, 1);
  1021.     }
  1022.  
  1023.       else if (x & ABSJMP)
  1024.     {
  1025.       /* This jmp may be a jump or a branch */
  1026.  
  1027.       check_operand (operand + i, Hmode ? 0xffffff : 0xffff, "@");
  1028.       if (operand[i].exp.X_add_number & 1)
  1029.         {
  1030.           as_warn ("branch operand has odd offset (%x)\n",
  1031.                operand->exp.X_add_number);
  1032.         }
  1033.       if (!Hmode)
  1034.         operand[i].exp.X_add_number = (short) operand[i].exp.X_add_number;
  1035.       fix_new_exp (frag_now,
  1036.                output - frag_now->fr_literal,
  1037.                4,
  1038.                &operand[i].exp,
  1039.                0,
  1040.                R_JMPL1);
  1041.     }
  1042.     }
  1043.  
  1044. }
  1045.  
  1046. /*
  1047.   try and give an intelligent error message for common and simple to
  1048.   detect errors
  1049.   */
  1050.  
  1051. static void
  1052. DEFUN (clever_message, (opcode, operand),
  1053.        struct h8_opcode *opcode AND
  1054.        struct h8_op *operand)
  1055. {
  1056.   struct h8_opcode *scan = opcode;
  1057.  
  1058.   /* Find out if there was more than one possible opccode */
  1059.  
  1060.   if ((opcode + 1)->idx != opcode->idx)
  1061.     {
  1062.       unsigned int argn;
  1063.  
  1064.       /* Only one opcode of this flavour, try and guess which operand
  1065.          didn't match */
  1066.       for (argn = 0; argn < opcode->noperands; argn++)
  1067.     {
  1068.       switch (opcode->args.nib[argn])
  1069.         {
  1070.         case RD16:
  1071.           if (operand[argn].mode != RD16)
  1072.         {
  1073.           as_bad ("destination operand must be 16 bit register");
  1074.           return;
  1075.  
  1076.         }
  1077.           break;
  1078.  
  1079.         case RS8:
  1080.  
  1081.           if (operand[argn].mode != RS8)
  1082.         {
  1083.           as_bad ("source operand must be 8 bit register");
  1084.           return;
  1085.         }
  1086.           break;
  1087.  
  1088.         case ABS16DST:
  1089.           if (operand[argn].mode != ABS16DST)
  1090.         {
  1091.           as_bad ("destination operand must be 16bit absolute address");
  1092.           return;
  1093.         }
  1094.           break;
  1095.         case RD8:
  1096.           if (operand[argn].mode != RD8)
  1097.         {
  1098.           as_bad ("destination operand must be 8 bit register");
  1099.           return;
  1100.         }
  1101.           break;
  1102.  
  1103.  
  1104.         case ABS16SRC:
  1105.           if (operand[argn].mode != ABS16SRC)
  1106.         {
  1107.           as_bad ("source operand must be 16bit absolute address");
  1108.           return;
  1109.         }
  1110.           break;
  1111.  
  1112.         }
  1113.     }
  1114.     }
  1115.   as_bad ("invalid operands");
  1116. }
  1117.  
  1118. /* This is the guts of the machine-dependent assembler.  STR points to a
  1119.    machine dependent instruction.  This funciton is supposed to emit
  1120.    the frags/bytes it assembles to.
  1121.    */
  1122.  
  1123.  
  1124.  
  1125. void
  1126. DEFUN (md_assemble, (str),
  1127.        char *str)
  1128. {
  1129.   char *op_start;
  1130.   char *op_end;
  1131.   unsigned int i;
  1132.   struct h8_op operand[2];
  1133.   struct h8_opcode *opcode;
  1134.   struct h8_opcode *prev_opcode;
  1135.  
  1136.   char *dot = 0;
  1137.   char c;
  1138.  
  1139.   /* Drop leading whitespace */
  1140.   while (*str == ' ')
  1141.     str++;
  1142.  
  1143.   /* find the op code end */
  1144.   for (op_start = op_end = str;
  1145.        *op_end != 0 && *op_end != ' ';
  1146.        op_end++)
  1147.     {
  1148.       if (*op_end == '.')
  1149.     {
  1150.       dot = op_end + 1;
  1151.       *op_end = 0;
  1152.       op_end += 2;
  1153.       break;
  1154.     }
  1155.     }
  1156.  
  1157.   ;
  1158.  
  1159.   if (op_end == op_start)
  1160.     {
  1161.       as_bad ("can't find opcode ");
  1162.     }
  1163.   c = *op_end;
  1164.  
  1165.   *op_end = 0;
  1166.  
  1167.   opcode = (struct h8_opcode *) hash_find (opcode_hash_control,
  1168.                        op_start);
  1169.  
  1170.   if (opcode == NULL)
  1171.     {
  1172.       as_bad ("unknown opcode");
  1173.       return;
  1174.     }
  1175.  
  1176.   /* We use to set input_line_pointer to the result of get_operands,
  1177.      but that is wrong.  Our caller assumes we don't change it.  */
  1178.  
  1179.   (void) get_operands (opcode->noperands, op_end, operand);
  1180.   *op_end = c;
  1181.   prev_opcode = opcode;
  1182.  
  1183.   opcode = get_specific (opcode, operand);
  1184.  
  1185.   if (opcode == 0)
  1186.     {
  1187.       /* Couldn't find an opcode which matched the operands */
  1188.       char *where = frag_more (2);
  1189.  
  1190.       where[0] = 0x0;
  1191.       where[1] = 0x0;
  1192.       clever_message (prev_opcode, operand);
  1193.  
  1194.       return;
  1195.     }
  1196.   if (opcode->size && dot)
  1197.     {
  1198.       if (opcode->size != *dot)
  1199.     {
  1200.       as_warn ("mismatch between opcode size and operand size");
  1201.     }
  1202.     }
  1203.  
  1204.   build_bytes (opcode, operand);
  1205.  
  1206. }
  1207.  
  1208. void
  1209. DEFUN (tc_crawl_symbol_chain, (headers),
  1210.        object_headers * headers)
  1211. {
  1212.   printf ("call to tc_crawl_symbol_chain \n");
  1213. }
  1214.  
  1215. symbolS *
  1216. DEFUN (md_undefined_symbol, (name),
  1217.        char *name)
  1218. {
  1219.   return 0;
  1220. }
  1221.  
  1222. void
  1223. DEFUN (tc_headers_hook, (headers),
  1224.        object_headers * headers)
  1225. {
  1226.   printf ("call to tc_headers_hook \n");
  1227. }
  1228.  
  1229. /* Various routines to kill one day */
  1230. /* Equal to MAX_PRECISION in atof-ieee.c */
  1231. #define MAX_LITTLENUMS 6
  1232.  
  1233. /* Turn a string in input_line_pointer into a floating point constant of type
  1234.    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
  1235.    emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
  1236.    */
  1237. char *
  1238. md_atof (type, litP, sizeP)
  1239.      char type;
  1240.      char *litP;
  1241.      int *sizeP;
  1242. {
  1243.   int prec;
  1244.   LITTLENUM_TYPE words[MAX_LITTLENUMS];
  1245.   LITTLENUM_TYPE *wordP;
  1246.   char *t;
  1247.   char *atof_ieee ();
  1248.  
  1249.   switch (type)
  1250.     {
  1251.     case 'f':
  1252.     case 'F':
  1253.     case 's':
  1254.     case 'S':
  1255.       prec = 2;
  1256.       break;
  1257.  
  1258.     case 'd':
  1259.     case 'D':
  1260.     case 'r':
  1261.     case 'R':
  1262.       prec = 4;
  1263.       break;
  1264.  
  1265.     case 'x':
  1266.     case 'X':
  1267.       prec = 6;
  1268.       break;
  1269.  
  1270.     case 'p':
  1271.     case 'P':
  1272.       prec = 6;
  1273.       break;
  1274.  
  1275.     default:
  1276.       *sizeP = 0;
  1277.       return "Bad call to MD_ATOF()";
  1278.     }
  1279.   t = atof_ieee (input_line_pointer, type, words);
  1280.   if (t)
  1281.     input_line_pointer = t;
  1282.  
  1283.   *sizeP = prec * sizeof (LITTLENUM_TYPE);
  1284.   for (wordP = words; prec--;)
  1285.     {
  1286.       md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
  1287.       litP += sizeof (LITTLENUM_TYPE);
  1288.     }
  1289.   return 0;
  1290. }
  1291.  
  1292. CONST char *md_shortopts = "";
  1293. struct option md_longopts[] = {
  1294.   {NULL, no_argument, NULL, 0}
  1295. };
  1296. size_t md_longopts_size = sizeof(md_longopts);
  1297.  
  1298. int
  1299. md_parse_option (c, arg)
  1300.      int c;
  1301.      char *arg;
  1302. {
  1303.   return 0;
  1304. }
  1305.  
  1306. void
  1307. md_show_usage (stream)
  1308.      FILE *stream;
  1309. {
  1310. }
  1311.  
  1312. int md_short_jump_size;
  1313.  
  1314. void
  1315. tc_aout_fix_to_chars ()
  1316. {
  1317.   printf ("call to tc_aout_fix_to_chars \n");
  1318.   abort ();
  1319. }
  1320.  
  1321. void
  1322. md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
  1323.      char *ptr;
  1324.      addressT from_addr;
  1325.      addressT to_addr;
  1326.      fragS *frag;
  1327.      symbolS *to_symbol;
  1328. {
  1329.   as_fatal ("failed sanity check.");
  1330. }
  1331.  
  1332. void
  1333. md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
  1334.      char *ptr;
  1335.      addressT from_addr, to_addr;
  1336.      fragS *frag;
  1337.      symbolS *to_symbol;
  1338. {
  1339.   as_fatal ("failed sanity check.");
  1340. }
  1341.  
  1342. void
  1343. md_convert_frag (headers, fragP)
  1344.      object_headers *headers;
  1345.      fragS *fragP;
  1346.  
  1347. {
  1348.   printf ("call to md_convert_frag \n");
  1349.   abort ();
  1350. }
  1351.  
  1352. valueT 
  1353. md_section_align (seg, size)
  1354.      segT seg;
  1355.      valueT size;
  1356. {
  1357.   return ((size + (1 << section_alignment[(int) seg]) - 1) & (-1 << section_alignment[(int) seg]));
  1358.  
  1359. }
  1360.  
  1361. void
  1362. md_apply_fix (fixP, val)
  1363.      fixS *fixP;
  1364.      long val;
  1365. {
  1366.   char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
  1367.  
  1368.   switch (fixP->fx_size)
  1369.     {
  1370.     case 1:
  1371.       *buf++ = val;
  1372.       break;
  1373.     case 2:
  1374.       *buf++ = (val >> 8);
  1375.       *buf++ = val;
  1376.       break;
  1377.     case 4:
  1378.       *buf++ = (val >> 24);
  1379.       *buf++ = (val >> 16);
  1380.       *buf++ = (val >> 8);
  1381.       *buf++ = val;
  1382.       break;
  1383.     default:
  1384.       abort ();
  1385.  
  1386.     }
  1387. }
  1388.  
  1389. void
  1390. DEFUN (md_operand, (expressionP), expressionS * expressionP)
  1391. {
  1392. }
  1393.  
  1394. int md_long_jump_size;
  1395. int
  1396. md_estimate_size_before_relax (fragP, segment_type)
  1397.      register fragS *fragP;
  1398.      register segT segment_type;
  1399. {
  1400.   printf ("call tomd_estimate_size_before_relax \n");
  1401.   abort ();
  1402. }
  1403.  
  1404. /* Put number into target byte order */
  1405.  
  1406. void
  1407. DEFUN (md_number_to_chars, (ptr, use, nbytes),
  1408.        char *ptr AND
  1409.        valueT use AND
  1410.        int nbytes)
  1411. {
  1412.   switch (nbytes)
  1413.     {
  1414.     case 4:
  1415.       *ptr++ = (use >> 24) & 0xff;
  1416.     case 3:
  1417.       *ptr++ = (use >> 16) & 0xff;
  1418.     case 2:
  1419.       *ptr++ = (use >> 8) & 0xff;
  1420.     case 1:
  1421.       *ptr++ = (use >> 0) & 0xff;
  1422.       break;
  1423.     default:
  1424.       abort ();
  1425.     }
  1426. }
  1427. long
  1428. md_pcrel_from (fixP)
  1429.      fixS *fixP;
  1430. {
  1431.   abort ();
  1432. }
  1433.  
  1434.  
  1435. void
  1436. tc_reloc_mangle (fix_ptr, intr, base)
  1437.      fixS *fix_ptr;
  1438.      struct internal_reloc *intr;
  1439.      bfd_vma base;
  1440.  
  1441. {
  1442.   symbolS *symbol_ptr;
  1443.  
  1444.   symbol_ptr = fix_ptr->fx_addsy;
  1445.  
  1446.   /* If this relocation is attached to a symbol then it's ok
  1447.      to output it */
  1448.   if (fix_ptr->fx_r_type == RELOC_32)
  1449.     {
  1450.       /* cons likes to create reloc32's whatever the size of the reloc..
  1451.        */
  1452.       switch (fix_ptr->fx_size)
  1453.     {
  1454.  
  1455.     case 2:
  1456.       intr->r_type = R_RELWORD;
  1457.       break;
  1458.     case 1:
  1459.       intr->r_type = R_RELBYTE;
  1460.       break;
  1461.     default:
  1462.       abort ();
  1463.  
  1464.     }
  1465.  
  1466.     }
  1467.   else
  1468.     {
  1469.       intr->r_type = fix_ptr->fx_r_type;
  1470.     }
  1471.  
  1472.   intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
  1473.   intr->r_offset = fix_ptr->fx_offset;
  1474.  
  1475.   if (symbol_ptr)
  1476.     intr->r_symndx = symbol_ptr->sy_number;
  1477.   else
  1478.     intr->r_symndx = -1;
  1479.  
  1480.  
  1481. }
  1482.  
  1483. tc_coff_sizemachdep ()
  1484. {
  1485.   abort ();
  1486. }
  1487.  
  1488. /* end of tc-h8300.c */
  1489.